package com.wym.charging.config;

import com.wym.charging.interceptor.LoginInterceptor;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author WangYimin 2024年12月24日 11:24:16
 */
@Configuration
public class WebConfiguration implements WebMvcConfigurer {
    @Autowired
    private LoginInterceptor loginInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new HandlerInterceptor() {
            @Override
            public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
                if ("/favicon.ico".equals(request.getRequestURI())){
                    response.setStatus(HttpStatus.NO_CONTENT.value()); // 设置状态码为204 No Content
                    return false;
                }
                return true;
            }
        }).addPathPatterns("/**");
        registry.addInterceptor(loginInterceptor).excludePathPatterns("/user/login",
                                                                      "/user/register",
                                                                      "/v3/api-docs/**",
                                                                      "/swagger-ui/**",
                                                                      "/swagger-ui.html",
                                                                      "/doc.html/**",
                                                                      "/swagger**/**",
                                                                      "/openapi/**",
                                                                      "/**/webjars/**");
    }
}
